#!/usr/bin/make -f

# list of flavors we build; each gets a builddir, a configure pass (configure
# args are defined below), a build pass, and an install pass
FLAVORS := main udeb

# current flavor we're building; this is only expanded in flavor specific
# targets
current_flavor = $*

# macro to get a value for the current flavor we're building; for example
# when building the main flavor, $(call flavor_get,CFLAGS) will expand to
# main_CFLAGS if it's set or to CFLAGS otherwise; pay attention to not adding
# superflous spaces when for the arguments of $(call ); only some vars can
# be expanded in this way though
flavor_get = $(or $($(current_flavor)_$(1)),$($(1)))

# Supported backends (as of Cairo 1.7.4):
#  (internal) Image
#  xlib       Xlib
#  pdf        PDF
#  ps         PostScript
#  svg        SVG
#
# Experimental and unsupported backends:
#  directfb  DirectFB (requires Build-Depend on libdirectfb-dev (>=0.9.25) )
#  glitz     OpenGL (glitz)
#  xcb       XCB  (requires Build-Depend on at least libxcb1-dev)
#
# Other platform backends are auto disabled: quartz/OSX, win32/Win32
#
# Features:
# --enable-png  PNG (default enabled)

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

configure_flags += \
--prefix=/usr \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--enable-pdf --enable-ps \
--enable-xlib \
--enable-png \
--disable-silent-rules --disable-shave \
--build=$(DEB_BUILD_GNU_TYPE)

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
configure_flags += --host=$(DEB_HOST_GNU_TYPE)
endif

main_configure_flags += \
$(configure_flags) \
--enable-xlib --enable-svg \
--enable-xcb

udeb_configure_flags += \
$(configure_flags) \
--disable-svg \
--disable-xcb

builddir = $(buildbasedir)/$(current_flavor)
buildbasedir = $(CURDIR)/debian/build

installdir = $(installbasedir)/$(current_flavor)
installbasedir = $(CURDIR)/debian/install

# default CFLAGS; these can be expanded with $(call flavor_get, )
CFLAGS += -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS_main += $(CFLAGS) -O0
CFLAGS_udeb += $(CFLAGS) -O0
else
CFLAGS_main += $(CFLAGS) -O2
CFLAGS_udeb += $(CFLAGS) -Os
endif

LDFLAGS += -Wl,-z,defs -Wl,-O1 -Wl,--as-needed

configure-common-stamp:
	dh_testdir
	# Generated by configure and breaks the build if already existing
	-test -r src/cairo-features.h && \
	  mv src/cairo-features.h src/cairo-features.h.orig
	touch $@

configure-stamp-%: configure-common-stamp
	dh_testdir
	mkdir -p $(builddir); \
	cd $(builddir); \
	$(CURDIR)/configure \
		CFLAGS="$(call flavor_get,CFLAGS)" \
		LDFLAGS="$(LDFLAGS)" \
		$(call flavor_get,configure_flags)
	touch $@

build: $(addprefix build-stamp-, $(FLAVORS))

build-stamp-%: configure-stamp-%
	dh_testdir
	$(MAKE) -C $(builddir)
	touch $@

clean:
	dh_testdir
	dh_testroot
	# restore files from backup
	[ ! -r src/cairo-features.h.orig ] || mv -f src/cairo-features.h.orig src/cairo-features.h
	rm -f *-stamp
	rm -rf $(buildbasedir) $(installbasedir)
	dh_clean

install-%: build-stamp-%
	$(MAKE) -C $(builddir) install DESTDIR=$(installdir)
	sed -i "/dependency_libs/ s/'.*'/''/" $(installdir)/usr/lib/*.la

install:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	for f in $(FLAVORS); do \
	    debian/rules install-$$f; \
	done

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i -A NEWS README AUTHORS
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -s
	dh_installdocs -s -A README NEWS AUTHORS
	dh_installchangelogs -s ChangeLog
	dh_installman -s
	dh_installexamples -s
	dh_link -s
	dh_strip -s --dbg-package=libcairo2-dbg -Nlibcairo2-udeb
	dh_strip -plibcairo2-udeb
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibcairo2 --add-udeb=libcairo2-udeb -V 'libcairo2 (>= 1.8.0-2)' -- -c4
	dh_installdeb -s
	dh_shlibdeps -s
	dh_perl -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install clean
